import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  strict: true,
  state: {
    products: [
      { name: "马云", price: 200 },
      { name: "马华腾", price: 100 },
      { name: "马荣", price: 20 },
      { name: "马小跳", price: 10 },
    ]
  },
  getters: {
    saleProducts(state) {
      var saleProducts = state.products.map(item => {
        return {
          name: "&&" + item.name + "**",
          price: item.price / 2
        }
      })
      return saleProducts
    }
  },
  mutations: {
    reductSale: (state, patload) => {
      state.products.forEach(item => {
        item.price -= patload;
      })
    }
  },
  actions: {
    reductSales: (context, patload) => {
      setTimeout(function() {
        context.commit("reductSale", patload)
      }, 2000)
    }
  }
})
